package com.sec.etech.ism.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sec.etech.common.constant.DBConstant;
import com.sec.etech.common.util.RequestHolder;
import com.sec.etech.ism.constant.SignatureConstant;
import com.sec.etech.ism.service.IsmMethodService;
import com.sec.etech.ism.service.IsmTaskService;
import com.sec.etech.ism.util.IsmStringUtil;
import com.sec.etech.sys.util.DbUtil;
import lombok.extern.slf4j.Slf4j;
import org.openbpm.base.api.exception.BusinessMessage;
import org.openbpm.base.core.util.StringUtil;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class IsmGetSelfCheckListInfo implements IsmMethodService {
    @Resource
    IsmTaskService ismTaskService;

    public IsmGetSelfCheckListInfo() {
    }

    @Override
    public JSONObject call(String sysCode, String serviceCode, JSONObject businessParams) throws Exception {
        JSONObject result = new JSONObject();
        this.checkAndGetData(sysCode, businessParams);
        JSONObject dataObj = this.getSelfCheckListInfo(sysCode, businessParams.getJSONObject(SignatureConstant.INPUT_PARAMS));
        ismTaskService.deleteTaskByParams(sysCode, serviceCode, serviceCode, null, businessParams.getString(SignatureConstant.INPUT_PARAMS));
        result.put("flag", "success");
        result.put("data", dataObj);
        return result;
    }

    //入参检查
    private void checkAndGetData(String sysCode, JSONObject busParams) throws Exception {
        if ("mes".equals(sysCode)) {
            String selfCheckListNum = busParams.getJSONObject(SignatureConstant.INPUT_PARAMS).getString("selfCheckListNum");
            if (StringUtil.isEmpty(selfCheckListNum)) {
                throw new BusinessMessage("请求中缺少selfCheckListNum参数，请检查！");
            }
        } else if ("qms".equals(sysCode)) {
            JSONObject object = busParams.getJSONObject(SignatureConstant.INPUT_PARAMS);
            if (StringUtil.isEmpty(object.getString("routeSerial"))
                    && StringUtil.isEmpty(object.getString("material_no"))) {
                throw new BusinessMessage("请求参数中routeSerial、material_no至少传一个，请检查！");
            }
            if (StringUtil.isEmpty(object.getString("operationNo"))) {
                throw new BusinessMessage("请求中缺少operationNo参数，请检查！");
            }
        }
    }

    //根据物料号获取路线卡信息
    private JSONObject getSelfCheckListInfo(String sysCode, JSONObject inputParams) {

        JdbcTemplate slaveJdbcTemplate = DbUtil.getJdbcTemplate(DBConstant.ETECH_SLAVE);
        JSONObject dataObj = new JSONObject();
        List<Map<String, Object>> list = null;
        if ("mes".equals(sysCode)) {
            String sql = "select serial_number,list_name,figure_file,measurement_item from spp_step_z_self_check_list_type " +
                    "where serial_number=? and active=1";
            list = slaveJdbcTemplate.queryForList(sql, inputParams.getString("selfCheckListNum"));
        } else if ("qms".equals(sysCode)) {
            String sql = "select serial_number,list_name,figure_file,measurement_item from spp_step_z_self_check_list_type " +
                    "where active=1 and id in (select st.self_check_list_id " +
                    " from spp_route_step st, spp_route_op op, spp_route_rev rev " +
                    " where st.route_op_id=op.route_op_id and op.route_rev_id=rev.route_rev_id " +
                    "  and rev.active=1 and rev.curr_ver=1 and st.self_check_list_id>0 ";
            if(StringUtil.isNotEmpty(inputParams.getString("routeSerial"))) {
                sql = sql + " and st.route_rev_id=rev.route_rev_id and rev.serial_num=? and op.op_no=?)";
                list = slaveJdbcTemplate.queryForList(sql, inputParams.getString("routeSerial"), inputParams.getString("operationNo"));
            }else if(StringUtil.isNotEmpty(inputParams.getString("material_no"))) {
                sql = sql + " and st.route_rev_id=rev.route_rev_id and rev.material_no=? and op.op_no=?)";
                list = slaveJdbcTemplate.queryForList(sql, inputParams.getString("material_no"), inputParams.getString("operationNo"));
            }
        }
        if (list != null && list.size() > 0) {
            for (Map<String, Object> map : list) {
                dataObj.put("selfCheckListNum", IsmStringUtil.getStrNullToEmpty(map.get("serial_number")));
                dataObj.put("selfCheckListName", IsmStringUtil.getStrNullToEmpty(map.get("list_name")));
                dataObj.put("measurementItem", IsmStringUtil.getStrNullToEmpty(map.get("measurement_item")));
                String fileUrl = "";
                if (StringUtil.isNotEmpty(map.get("figure_file"))) {
                    fileUrl = RequestHolder.getUrl() + "/sys/sysFile/preview.html?audience=drawing&withWatermark=1&fileId=" + map.get("figure_file").toString();
                }
                dataObj.put("fileUrl", fileUrl);
                dataObj.put("selfCheckListNum", IsmStringUtil.getStrNullToEmpty(map.get("serial_number")));
            }
        }
        return dataObj;
    }

    @Override
    public String methodName() {
        return "getSelfCheckListInfo";
    }
}
